package com.github.appintro.example.ui.custom.fragments;

import com.github.appintro.SlidePolicy;

import com.github.appintro.example.ui.ResourceTable;
import com.github.appintro.utile.ResUtil;
import com.github.appintro.utile.ShapeUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.Element;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CustomSlidePolicyFragment extends DirectionalLayout implements SlidePolicy {
    private Checkbox checkBox;
    private Context context;
    private DirectionalLayout directionalLayout;

    public CustomSlidePolicyFragment(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public CustomSlidePolicyFragment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_intro_slide_policy, this, true);
        checkBox = (Checkbox) directionalLayout.findComponentById(ResourceTable.Id_check_box);
        checkBox.setButtonElement(ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_unselect));
        checkBox.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (checkBox.isChecked()) {
                    checkBox.setButtonElement(ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_unselect));
                } else {
                    checkBox.setButtonElement(ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_select));
                }
            }
        });
    }

    public CustomSlidePolicyFragment(Context context) {
        this(context, null);
    }

    public boolean isPolicyRespected() {
        return checkBox.isChecked();
    }

    public void onUserIllegallyRequestedNextPage() {
        String txt = "not resource data";
        ResourceManager resourceManager = context.getResourceManager();
        try {
            Element element = resourceManager.getElement(ResourceTable.String_please_select_the_checkbox_before_proceeding);
            txt = element.getString();
        } catch (IOException | NotExistException | WrongTypeException ex) {
            Logger.getLogger(CustomSlidePolicyFragment.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
        showToast(context, txt);
    }

    private void showToast(Context abilityContext, String str) {
        Text text = new Text(abilityContext);
        text.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        text.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        text.setTextSize(40);
        text.setText(str);
        text.setMultipleLine(true);
        text.setTextAlignment(TextAlignment.CENTER);
        DirectionalLayout directionalLt = new DirectionalLayout(abilityContext);
        directionalLt.setBackground(ShapeUtil.createDrawable(ResUtil.getColor(abilityContext, com.github.appintro.ResourceTable.Color_appintro_statusbar_color), 8));
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig
                (DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        layoutConfig.setMarginBottom(100);
        directionalLt.setLayoutConfig(layoutConfig);
        directionalLt.setPadding(20, 30, 20, 30);
        directionalLt.addComponent(text);
        ToastDialog toastDialog = new ToastDialog(abilityContext);
        toastDialog.setComponent(directionalLt).setDuration(3000).setAutoClosable(true)
                .setAlignment(LayoutAlignment.BOTTOM).setTransparent(true).show();
    }
}
